package com.example.custom_viewgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class MyLinearLayout extends ViewGroup {

    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        int currentHeight = 0;
        for (int i = 0 ; i < count ; i++) {
            View view = getChildAt(i);
            int height = view.getMeasuredHeight();
            int width  = view.getMeasuredWidth();
            view.layout(l, currentHeight, l + width, currentHeight + height);
            currentHeight += height;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 将所有的子View进行测量，这会触发每个子View的onMeasure函数
        // 注意要与measureChild区分，measureChild是对单个view进行测量
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        final int widthMode  = MeasureSpec.getMode(widthMeasureSpec);
        final int width      = MeasureSpec.getSize(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int height     = MeasureSpec.getSize(heightMeasureSpec);
        int childCount = getChildCount();
        // 如果没有子View,当前ViewGroup没有存在的意义，不用占用空间
        if (childCount == 0) {
            setMeasuredDimension(0, 0);
        }else {
            // 如果宽高都是包裹内容
            if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
                // 我们将高度设置为所有子View的高度相加，宽度设为子View中最大的宽度
                int groupWidth  = getMaxWidth();
                int groupHeight = getTotalHeight();
                setMeasuredDimension(groupWidth, groupHeight);
            }else if (widthMode == MeasureSpec.AT_MOST) {   // 如果只有宽度是包裹内容
                // 宽度设置为子View中宽度最大的值，高度设置为ViewGroup自己的测量值
                setMeasuredDimension(getMaxWidth(), height);
            }else if (heightMode == MeasureSpec.AT_MOST) {  // 如果只有高度是包裹内容
                // 宽度设置为ViewGroup自己的测量宽度，高度设置为所有子View的高度总和
                setMeasuredDimension(width, getTotalHeight());
            }else {
                setMeasuredDimension(width, height);
            }
        }
    }

    /**
     * 获取子View中宽度最大的值
     */
    private int getMaxWidth() {
        int count = getChildCount();
        int maxWidth = 0;
        for (int i = 0 ; i < count ; i ++){
            int currentWidth = getChildAt(i).getMeasuredWidth();
            if (maxWidth < currentWidth){
                maxWidth = currentWidth;
            }
        }
        return maxWidth;
    }

    /**
     * 将所有子View的高度相加
     */
    private int getTotalHeight() {
        int count = getChildCount();
        int totalHeight = 0;
        for (int i = 0 ; i < count ; i++) {
            totalHeight += getChildAt(i).getMeasuredHeight();
        }
        return totalHeight;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

    }
}
